package com.qyhl.webtv.module_circle.circle.circleshield;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.glide.GlideCircleTransform;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.module_circle.R;
import com.qyhl.webtv.module_circle.R2;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by qyhl on 2018/3/7.
 * 好友屏蔽列表
 */

@Route(path = ARouterPathConstant.CIRCLE_SHIELD)
public class CircleShieldActivity extends BaseActivity implements CircleShieldContract.CircleShieldView {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.back_btn)
    ImageView backBtn;

    private EmptyWrapper emptyWrapper;
    private CommonAdapter mAdapter;

    private List<CircleHomeBean.User> mDataList;

    private CircleShieldPresenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.circle_activity_circle_shield;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new CircleShieldPresenter(this);
        initView();
    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getShieldList();
    }

    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        mTitle.setText("屏蔽列表");

        mDataList = new ArrayList<>();

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setEnableLoadMore(false);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        //添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.circle_recycleview_divider2));
        mRecyclerView.addItemDecoration(divider);

        mAdapter = new CommonAdapter<CircleHomeBean.User>(this, R.layout.circle_item_circle_shield, mDataList) {

            @Override
            protected void convert(ViewHolder holder, final CircleHomeBean.User user, int position) {
                ImageView headIcon = holder.getView(R.id.head_icon);
                TextView nickName = holder.getView(R.id.nickName);
                final ImageView status = holder.getView(R.id.status);

                Glide.with(CircleShieldActivity.this)
                        .load(user.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default)
                                .transform(new GlideCircleTransform(CircleShieldActivity.this)))
                        .into(headIcon);

                nickName.setText(user.getNickName());

                status.setImageResource(R.drawable.circle_shield_off);

                status.setOnClickListener(v -> {
                    if (status.getDrawable().getCurrent().getConstantState().equals(ContextCompat.getDrawable(CircleShieldActivity.this, R.drawable.circle_shield_off).getConstantState())) {
                        mPresenter.undoBlock(user.getUsername(), status);
                    } else {
                        mPresenter.doBlock(user.getUsername(), status);
                    }
                });

            }
        };

        emptyWrapper = new EmptyWrapper(mAdapter);
        emptyWrapper.setEmptyView(R.layout.circle_layout_circleuser_emptyview);
        mRecyclerView.setAdapter(emptyWrapper);


    }

    /**
     * 设置监听
     */
    private void setListeners() {
        //重载
        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getShieldList();
        });

        //刷新
        refresh.setOnRefreshListener(refreshlayout -> mPresenter.getShieldList());

        backBtn.setOnClickListener(v -> finish());
    }

    @Override
    public void getListSuccess(List<CircleHomeBean.User> beanList) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);

        mDataList.clear();
        mDataList.addAll(beanList);

        emptyWrapper.notifyDataSetChanged();
    }

    @Override
    public void setNetError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~~");
        loadMask.setNoNetworkText(msg);
    }

    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~~");
        loadMask.setErrorText(msg);
    }

    @Override
    public void blockSuccess(String msg, ImageView imageView) {
        Toasty.normal(this, msg).show();
        imageView.setImageResource(R.drawable.circle_shield_off);
    }

    @Override
    public void blockError(String msg) {
        Toasty.normal(this, msg).show();
    }

    @Override
    public void unBlockSuccess(String msg, ImageView imageView) {
        Toasty.normal(this, msg).show();
        imageView.setImageResource(R.drawable.circle_shield_on);
    }

    @Override
    public void unBlockError(String msg) {
        Toasty.normal(this, msg).show();
    }

}
